package com.lzx.iteam;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.lzx.iteam.base.FindViewById;
import com.lzx.iteam.base.MyBaseActivity;
import com.lzx.iteam.bean.DeviceMsgData;
import com.lzx.iteam.net.AsynHttpClient;
import com.lzx.iteam.net.GetAttendanceCalendarMsg;
import com.lzx.iteam.net.GetExceptionDeviceMsg;
import com.lzx.iteam.net.LocalLogin;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * **********************************
 * User: zhangshuai
 * Date: 2016年 05月 06日
 * Time: 上午10:30
 *
 * @QQ : 1234567890
 * **********************************
 */
public class AttendanceDeviceErrorActivity extends MyBaseActivity implements View.OnClickListener {

    @FindViewById(id = R.id.ll_attendance_devices_back)
    private LinearLayout mLayoutBack;
    @FindViewById(id = R.id.tv_attendance_error_devices_name)
    private TextView mTvErrorName;
    @FindViewById(id = R.id.tv_attendance_error_devices_id)
    private TextView mTvErrorId;
    @FindViewById(id = R.id.tv_attendance_devices_name)
    private TextView mTvName;
    @FindViewById(id = R.id.tv_attendance_devices_id)
    private TextView mTvId;

    private String mGroupId, mDeviceId,mSetUid;

    private final int GET_DEVICE_MSG = 1000;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {
                case GET_DEVICE_MSG:
                    if (msg.arg1 == 0) {
                        ArrayList<DeviceMsgData> deviceMsgDatas = (ArrayList<DeviceMsgData>)msg.obj;
                       for(DeviceMsgData deviceMsgData:deviceMsgDatas){
                           if("1".equals(deviceMsgData.getGroupDefault())){
                               mTvName.setText(deviceMsgData.getDeviceName());
                               mTvId.setText("设备号:"+deviceMsgData.getDeviceId());
                           }else{
                               mTvErrorName.setText(deviceMsgData.getDeviceName());
                               mTvErrorId.setText("设备号:"+deviceMsgData.getDeviceId());
                           }
                       }


                    } else {
                        if (msg.arg1 == AsynHttpClient.ERROR_LOCAL) {
                            Toast.makeText(mActivity, R.string.active_failure_no_network,
                                    Toast.LENGTH_LONG).show();
                        } else {
                            Toast.makeText(mActivity, (String) msg.obj,
                                    Toast.LENGTH_LONG).show();
                        }
                    }
                    break;
            }
        }
    };


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState, R.layout.attendance_devices_error_layout);
    }

    @Override
    public void initListener() {
        mLayoutBack.setOnClickListener(this);
    }

    @Override
    public void initData() {
        Intent intent = getIntent();
        mGroupId = intent.getStringExtra("groupId");
        mDeviceId = intent.getStringExtra("deviceId");
        mSetUid = intent.getStringExtra("setUid");
        getAttendanceCalendarMsg();

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_attendance_devices_back:
                finish();
                break;
        }
    }


    private void getAttendanceCalendarMsg() {
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_SID, LocalLogin.getInstance().mSid));
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_DEVICE_ID, mDeviceId));
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_GROUP_ID, mGroupId));
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_SET_UID, mSetUid));
        GetExceptionDeviceMsg msg = new GetExceptionDeviceMsg(mHandler.obtainMessage(GET_DEVICE_MSG));
        msg.mParams = nvps;
        msg.mApi = AsynHttpClient.API_EVENT_ATTENDANCE_GET_EXCEPTION_DEVICE;
        AsynHttpClient.getInstance(this).execEventHttp(msg);
    }

}
